<script lang="ts" setup>
import { ref } from 'vue';

import { useVbenModal } from '@vben/common-ui';

import { useFs } from '@fast-crud/fast-crud';

import { useMessage } from '#/hooks/web/useMessage';

import { createCrudOptions } from '../data/to-examine-crud';

defineOptions({
  name: '审核',
  inheritAttrs: false,
});
const emit = defineEmits<Emits>();

interface Emits {
  (e: 'success'): void;
}

const formRef = ref();
const { createMessage } = useMessage();
const { crudBinding } = useFs({ createCrudOptions });

const [Modal, modalApi] = useVbenModal({
  async onOpened() {
    formRef.value.setFormData({ id: modalApi.getData()?.record?.id });
  },
  async onConfirm() {
    try {
      modalApi.setState({ loading: true, confirmLoading: true });
      await formRef.value.submit();

      createMessage.success('审核成功');
      modalApi.close();
      emit('success');
    } finally {
      modalApi.setState({ loading: false, confirmLoading: false });
    }
  },
});
defineExpose(modalApi);
</script>

<template>
  <Modal title="审核" class="w-[60%]">
    <div class="p-5">
      <FsForm ref="formRef" v-bind="crudBinding.editForm" />
    </div>
  </Modal>
</template>
